package com.peanut.baby.mvp.subscribe;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.PTag;
import com.peanut.devlibrary.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

public class TagSubscribePresenter implements TagSubscribeContract.Presenter{
    private TagSubscribeContract.View view;
    private BaseActivity                activity;

    public TagSubscribePresenter(TagSubscribeContract.View view, BaseActivity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getTags() {
        Observable observable = RetrofitClient.getInstance().getTagList(InitManager.getInstance().getUserId());
        observable.compose(BaseScheduler.<BaseResponse<List<PTag>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<PTag>>() {
                    @Override
                    protected void onHandleSuccess(List<PTag> o) {
                        view.onTagsGet(true,o,"'");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onTagsGet(false,null,msg);
                    }

                });
    }
    @Override
    public void subscribeTags(final List<PTag> tags) {
        List<Integer> ids = new ArrayList<>();
        for(int i=0;i<tags.size();i++){
            ids.add(tags.get(i).id);
        }
        Observable observable = RetrofitClient.getInstance().subscribeTag(InitManager.getInstance().getUserId(),ids,InitManager.getInstance().getUser().userToken);
        observable.compose(BaseScheduler.<BaseResponse<List<PTag>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<PTag>>() {
                    @Override
                    protected void onHandleSuccess(List<PTag> o) {
                        InitManager.getInstance().setTags(tags);
                        view.onSubscribeResult(true,"'");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onSubscribeResult(false,msg);
                    }

                });
    }

    /**
     * 月经、卵泡、精子、内分泌、多囊、卵巢、子宫、输卵管、精神调节、
     * 生活调节、运动锻炼、营养均衡、体重、二胎、大龄、久备不孕、生男生女、流产、宫外孕、
     * 妇科炎症、妇科疾病、中医调理、备孕检查、备孕食谱、备孕技巧、难孕难育、试管婴儿、已怀孕、其它
     * @return
     */
    List<PTag> getTestTags(){
        List<PTag> tags = new ArrayList<>();
        tags.add(new PTag(1,"月经"));
        tags.add(new PTag(2,"卵泡"));
        tags.add(new PTag(3,"精子"));
        tags.add(new PTag(4,"内分泌"));
        tags.add(new PTag(5,"多囊"));
        tags.add(new PTag(6,"卵巢"));
        tags.add(new PTag(7,"子宫"));
        tags.add(new PTag(8,"输卵管"));
        tags.add(new PTag(9,"精神调节"));
        tags.add(new PTag(10,"生活调节"));
        tags.add(new PTag(11,"运动锻炼"));
        tags.add(new PTag(12,"营养均衡"));
        tags.add(new PTag(13,"体重"));
        tags.add(new PTag(14,"二胎"));
        tags.add(new PTag(15,"大龄"));
        tags.add(new PTag(16,"久备不孕"));
        tags.add(new PTag(17,"生男生女"));
        tags.add(new PTag(18,"流产"));
        tags.add(new PTag(19,"宫外孕"));
        tags.add(new PTag(20,"妇科炎症"));
        tags.add(new PTag(21,"妇科疾病"));
        tags.add(new PTag(22,"中医调理"));
        tags.add(new PTag(23,"备孕检查"));
        tags.add(new PTag(24,"备孕食谱"));
        tags.add(new PTag(25,"备孕技巧"));
        tags.add(new PTag(26,"难孕难育"));
        tags.add(new PTag(27,"试管婴儿"));
        tags.add(new PTag(28,"已怀孕"));
        tags.add(new PTag(29,"其它"));

        return tags;
    }
}
